//
//  ByLotteryNoticeDetailVC.m
//  caipiao_ios
//
//  Created by mc on 2018/8/16.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByLotteryNoticeDetailVC.h"
#import "ByNoticeDetailHeadView.h"
#import "ByBetRecordTitleView.h"
#import "ByNoticeDetailThreeView.h"
#import "ByNoticeHistoryListVC.h"
@interface ByLotteryNoticeDetailVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;


@end

@implementation ByLotteryNoticeDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.title = @"开奖详情";
    [self addLeftBackBarButtonItem];
    
    if (self.isFromHostoryList) {
        
        [self loadData];
        
    }else {
        
        [self initViews];
        
    }
    
    [self addBYItemviews:@[@"历史开奖"] mode:ByNavigationItemModeText  position:ByNavigationItemPositionModeRight];
    // Do any additional setup after loading the view.
}


- (void)loadData{
    
    ByNetModel *netModel = [[ByNetModel alloc] init];
    netModel.ID = self.ID;
    [ByApiOther drawNoticeDetail:netModel success:^(ByNetBaseResult *result) {
        [ByCoreToolAlert hideHud];
        
        SLog(@"开奖详情 = %@", result.data);
        NSDictionary *dic = result.data[@"data"];
        self.netModel = [[ByNetModel alloc] initContentWithDic:dic];
        
        NSMutableArray *redArray = [ByLotteryAlgorithmHandle getModelsArrayWithArray:dic[@"red_num"] WithColor:BY_THEME_COLOR];
        NSMutableArray *blueArray = [ByLotteryAlgorithmHandle getModelsArrayWithArray:dic[@"blue_num"] WithColor:BY_Blue_COLOR];
        NSMutableArray *numArray = [NSMutableArray array];
        for (ByNetModel *model in redArray) {
            [numArray addObject:model];
        }
        
        for (ByNetModel *model in blueArray) {
            [numArray addObject:model];
        }
        self.netModel.numArray = numArray;
        
        
        //双色球
        if (self.netModel.category_id.integerValue == 1001){
            
            NSArray *contentArray = dic[@"content"];
            self.netModel.contentArray = contentArray;
            
        }else if (self.netModel.category_id.integerValue == 2001){
            
            self.netModel.contentDic = dic[@"content"];
        }
        
        [self initViews];
        
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showFailureWithText:result.msg];
    }];
    
}


- (void)rightItemsAction:(NSInteger)rightIndex{
    
    ByNoticeHistoryListVC *vc = [[ByNoticeHistoryListVC alloc] init];
    vc.netModel = self.netModel;
    [self by_normalPush:vc hasNavi:YES];
    
}

- (void)initViews{
    
    ByNoticeDetailHeadView *headView = [[ByNoticeDetailHeadView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    headView.netModel = self.netModel;
    
    self.view.backgroundColor = BY_BACKGROUND_COLOR;
    self.tableView = [[UITableView alloc] init];
    self.tableView.backgroundColor = BY_BACKGROUND_COLOR;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        
        if (@available(ios 11.0,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(0);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(0);
        } else {
            make.top.equalTo(self.view).offset(0);
            make.bottom.equalTo(self.view).offset(0);
        }
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.tableHeaderView = headView;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    ByBetRecordTitleView *titleView = [[ByBetRecordTitleView alloc] init];
    [cell.contentView addSubview:titleView];
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cell.contentView).offset(0);
        make.right.equalTo(cell.contentView).offset(0);
        make.top.equalTo(cell.contentView).offset(0);
        make.height.mas_equalTo(@40);
    }];
    titleView.titleLabel.text = @"开奖详情";
    titleView.titleLabel.textColor = BY_THEME_COLOR;
    titleView.dottedLine.hidden = YES;
    
    NSArray *titleAry = @[@"一",@"二",@"三",@"四",@"五",@"六"];
    
    for (int i = 0 ; i < 7; i++) {
        
        
        ByNoticeDetailThreeView *valueView = [[ByNoticeDetailThreeView alloc] initWithFrame:CGRectMake(0, 40 + i * 40, SCREEN_WIDTH, 40)];
        [cell.contentView addSubview:valueView];
        if (i % 2 == 1) {
            valueView.backgroundColor = rgba(248, 248, 248, 1);
        }
        
        if (i == 0) {
            valueView.leftLabel.text = @"奖项";
            valueView.centerLabel.text = @"中奖注数";
            valueView.rightLabel.text = @"每注奖金(元)";
        }else{
            
            valueView.rightLabel.textColor = BY_THEME_COLOR;
            valueView.leftLabel.text = [NSString stringWithFormat:@"%@等奖", titleAry[i - 1]];
            if (_netModel.category_id.integerValue == 1001) {
                
                if (_netModel.contentArray.count > 5) {
                    NSArray *array = [_netModel.contentArray objectAtIndex:i - 1];
                    valueView.centerLabel.text = [array objectAtIndex:1];
                    valueView.rightLabel.text = [NSString balanceFormatFromStr:[array lastObject]];
                }
                
            }else if (_netModel.category_id.integerValue == 2001){
                
                NSArray *array = _netModel.contentDic[[titleAry objectAtIndex:i - 1]][@"j"];
                valueView.centerLabel.text = [array firstObject];
                valueView.rightLabel.text = [NSString balanceFormatFromStr:[array lastObject]];
                
                
            }
            
            
        }
        
        
        UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, i * 40, SCREEN_WIDTH, 1)];
        [cell.contentView addSubview:line];
        line.backgroundColor = BY_LINE_COLOR;
        
        
    }
    
    for (int i = 1; i < 3; i++) {
        UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 3.0 * i , 40, 1, 280)];
        [cell.contentView addSubview:line];
        line.backgroundColor = BY_LINE_COLOR;
    }
    
    
    return cell;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 320;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
